<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Admin\Model\Thongtin;
use Admin\Form\ThongtinForm;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;
use Zend\Validator\File\Size;
use Zend\Validator\File\Extension;


class ThongtinController extends AbstractActionController {

    protected $thongtinTable;

	public function kiemtra(){
		$module = "thongtin";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$sessionUser = new Container('user');
		if(!$sessionUser->tendangnhap){
			return $this->redirect()->toRoute('auth');
		}
		else if($sessionUser->tendangnhap && $sessionUser->quyen != 1){
			return $this->redirect()->toRoute('home');
		}
	}
    
	public function indexAction() {
		$this->kiemtra();
		return $this->redirect()->toRoute('thongtin',array('action'=>'edit'));
	}
    public function editAction() {
		$this->kiemtra();
		
        $id = 1;
        
		$viewModel = new ViewModel();
        $thongtin = $this->getThongtinTable()->getThongtin($id);
		if (!$thongtin) {
            return $this->redirect()->toRoute('thongtin');
        }

		$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
        $form = new ThongtinForm($dbAdapter);
        $form->bind($thongtin);
        $form->get('submit')->setAttribute('value', 'Cập nhật');
		

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
				
                	$this->getThongtinTable()->saveThongtin($thongtin);

					// Redirect to list of lops
					return $this->redirect()->toRoute('thongtin');
				
            }
        }

        return array(
            'id' => $id,
            'form' => $form,
        );
    }

    

    public function getThongtinTable() {
        if (!$this->thongtinTable) {
            $sm = $this->getServiceLocator();
            $this->thongtinTable = $sm->get('Admin\Model\ThongtinTable');
        }
        return $this->thongtinTable;
    }

}
